using Microsoft.EntityFrameworkCore;
using AdminSystem.API.Data;
using AdminSystem.API.DTOs;
using AdminSystem.API.Models;
using AutoMapper;

namespace AdminSystem.API.Services
{
    public class ModuleService : IModuleService
    {
        private readonly ApplicationDbContext _context;
        private readonly IMapper _mapper;

        public ModuleService(ApplicationDbContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        public async Task<List<ModuleDto>> GetAllModulesAsync()
        {
            var modules = await _context.Modules
                .Include(m => m.Children)
                .Where(m => m.IsActive)
                .OrderBy(m => m.SortOrder)
                .ToListAsync();

            return _mapper.Map<List<ModuleDto>>(modules);
        }

        public async Task<List<ModuleDto>> GetModuleTreeAsync()
        {
            var modules = await _context.Modules
                .Include(m => m.Children)
                .Where(m => m.IsActive && m.ParentId == null)
                .OrderBy(m => m.SortOrder)
                .ToListAsync();

            return _mapper.Map<List<ModuleDto>>(modules);
        }

        public async Task<ModuleDto?> GetModuleByIdAsync(int id)
        {
            var module = await _context.Modules
                .Include(m => m.Children)
                .FirstOrDefaultAsync(m => m.Id == id);

            return module == null ? null : _mapper.Map<ModuleDto>(module);
        }

        public async Task<ModuleDto> CreateModuleAsync(CreateModuleDto createModuleDto)
        {
            var module = new Module
            {
                Name = createModuleDto.Name,
                Description = createModuleDto.Description,
                Icon = createModuleDto.Icon,
                Route = createModuleDto.Route,
                ParentId = createModuleDto.ParentId,
                SortOrder = createModuleDto.SortOrder,
                IsActive = true,
                CreatedAt = DateTime.UtcNow
            };

            _context.Modules.Add(module);
            await _context.SaveChangesAsync();

            return await GetModuleByIdAsync(module.Id) ?? throw new InvalidOperationException("Failed to retrieve created module");
        }

        public async Task<ModuleDto?> UpdateModuleAsync(int id, UpdateModuleDto updateModuleDto)
        {
            var module = await _context.Modules.FindAsync(id);
            if (module == null)
                return null;

            module.Name = updateModuleDto.Name;
            module.Description = updateModuleDto.Description;
            module.Icon = updateModuleDto.Icon;
            module.Route = updateModuleDto.Route;
            module.ParentId = updateModuleDto.ParentId;
            module.SortOrder = updateModuleDto.SortOrder;
            module.IsActive = updateModuleDto.IsActive;
            module.UpdatedAt = DateTime.UtcNow;

            await _context.SaveChangesAsync();

            return await GetModuleByIdAsync(id);
        }

        public async Task<bool> DeleteModuleAsync(int id)
        {
            var module = await _context.Modules.FindAsync(id);
            if (module == null)
                return false;

            module.IsActive = false;
            module.UpdatedAt = DateTime.UtcNow;
            await _context.SaveChangesAsync();

            return true;
        }
    }
}
